#!/usr/bin/env python
# coding=utf-8

import os, sys, re, traceback, logging
import urllib
import time
import datetime
import getopt
import math
import urllib2
from Route import *

depcity_dict = {'65':'北京','66':'长沙','17':'成都','1':'重庆','462':'长春','15':'大连','12':'福州','10':'广州','7':'桂林','6':'贵阳','83':'哈尔滨','2':'海口','5':'杭州','3':'济南','8':'昆明','105':'拉萨','181':'绵阳','104':'南昌','4':'南京','22':'南宁','18':'普陀山','9':'青岛','2':'三亚','61':'上海','14':'沈阳','25':'深圳','102':'石家庄','161':'太原','16':'天津','121':'温州','103':'武汉','101':'厦门','84':'西安','24':'烟台','64':'郑州','361':'珠海'}

page_total_re = re.compile(r'''共<font\scolor="\S+">(\d+?)</font>页''', re.X|re.S)

route_re = re.compile(r'''
             <div\s+class="trip_1">([\s\S]+?)</div>\s+</div>
             ''', re.X|re.S)
url_re = re.compile(r'''
             <a\s+href="([\S]+?)"\s+target="_blank"><strong>
             ''', re.X|re.S)
title_re = re.compile(r'''
             blank"><strong>([\s\S]+?)</strong></a>
             ''', re.X|re.S)
price_re = re.compile(r'''
             <strong>\s+(\d+)\s+元起</strong>
             ''', re.X|re.S)
days_re = re.compile(r'''
                                  游玩天数\s+(\d+)天
             ''', re.X|re.S)
detail_re = re.compile(r'''
             <span\sclass="goldtext">([\s\S]+?)</span>                     
             ''', re.X|re.S)
schedule_re = re.compile(r'''
             <td\sclass="font92">([\s\S]+?)<br><br></td>
             ''', re.X|re.S)

url_template = "http://www.china-sss.com/torder/search_iframe_trip.asp?Dest=&DepCity=%s&price=&daynu=&rank=&page=%s"



def do_crawl():
    try:
        for depid,depcity in depcity_dict.items():
            html = urllib2.urlopen(url_template % (depcity.decode("utf-8").encode("gbk"),'1')).read().decode("gbk").encode("utf-8")
            if page_total_re.search(html):
                page_total = string.atoi(page_total_re.search(html).group(1))
                for page_no in range(1,page_total):
                    html = urllib2.urlopen(url_template % (depcity.decode("utf-8").encode("gbk"),page_no)).read().decode("gbk").encode("utf-8")
                    route_list = route_re.findall(html)
                    for route_html in route_list:
                        route = Route()
                        route.site = 25
                        route.supplier_url = 'http://www.china-sss.com/'
                        route.supplier = "春秋航空"
                        route.go_t = route.back_t = "-"
                        route.type = "组团游"
                        route.dates = "天天发团"
                        route.outcity = depcity
                        if title_re.search(route_html):
                            route.title = title_re.search(route_html).group(1)
                        if price_re.search(route_html):
                            route.price = price_re.search(route_html).group(1)
                        if days_re.search(route_html):
                            route.days = days_re.search(route_html).group(1)
                        if detail_re.search(route_html).group(1):
                            route.detail = detail_re.search(route_html).group(1).strip()
                        if url_re.search(route_html):
                            route_url = url_re.search(route_html).group(1)
                        route.rout_url = 'http://www.china-sss.com' + route_url
                        page = urllib2.urlopen(route.rout_url).read().decode("gbk").encode("utf-8")
                        do_parse(page,route)
                        print >>sys.stderr, 'updating', route.supplier, route.title
                        route.updRoute()
                        route.updRoute_Schedules()
    except:
        print traceback.format_exc(sys.exc_info())

def do_parse(html,route):
    schedule_list = schedule_re.findall(html)
    for parthtml in schedule_list:
        schedule = Schedule()
        if re.compile(r'''<strong>第(\d+)天</strong>''', re.X|re.S).search(parthtml):
            schedule.s_num = re.compile(r'''<strong>第(\d+)天</strong>''', re.X|re.S).search(parthtml).group(1)
        hotel_temp = re.compile(r'''住宿安排：([\s\S]+?)<DIV>''', re.X|re.S).search(parthtml)
        if hotel_temp:
            schedule.s_hotel = hotel_temp.group(1)
        dinner_temp = re.compile(r'''餐食安排：([\s\S]+?)<DIV>''', re.X|re.S).search(parthtml)
        if dinner_temp:
            str_temp = dinner_temp.group(1)
            if str_temp.find("自理早餐") != -1 or str_temp.find("早餐：自理") != -1:
                schedule.s_brf = '0'
            else:
                schedule.s_brf = '1'
            if str_temp.find("自理午餐") != -1 or str_temp.find("午餐：自理") != -1:
                schedule.s_lunch = '0'
            else:
                schedule.s_lunch = '1'
            if str_temp.find("自理晚餐") != -1 or str_temp.find("晚餐：自理") != -1:
                schedule.s_dinner = '0'
            else:
                schedule.s_dinner = '1'
        else: 
            schedule.s_brf = schedule.s_lunch = schedule.s_dinner = '0'
        place_temp = re.compile(r'''right;"></a>([\s\S]+?)<DIV>''', re.X|re.S).search(parthtml)
        if place_temp:
            strs = place_temp.group(1).split('—')
            schedule.s_place = strs[0]
        brief_temp = re.compile(r'''right;"></a>([\s\S]+?)<DIV>★住宿安排''', re.X|re.S).search(parthtml)
        if brief_temp:
            schedule.s_brief = brief_temp.group(1)
        route.schedules.append(schedule)
    return 
        

if __name__ == '__main__':
    script_path = os.path.dirname(os.path.realpath(__file__))
    os.chdir(script_path)
    do_debug = False

    try:
        opts, args = getopt.gnu_getopt(sys.argv[1:], 'hdno:p:', ['help', 'debug', 'no-cache', 'only-cache', 'provider'])
    except getopt.GetoptError:
        usage()
        sys.exit()

    for opt, arg in opts:
        if opt in ('-h', '--help'):
            usage() 
            sys.exit()
        elif opt in ('-d', '--debug'):
            do_debug = True
        elif opt in ('-n', '--no-cache'):
            use_cache = False
        elif opt in ('-o', '--only-cache'):
            if arg.lower() in ('no', 'n', '0'):
                only_cache = False
            else:
                only_cache = True
        elif opt in ('-p', '--provider'):
            pass

    if do_debug:
        import pdb
        pdb.set_trace()
    do_crawl()
